package com.bnlr.lrparking.appui.stall;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.appui.search.SeachActiity;
import com.bnlr.lrparking.appui.login.VerificaActivity;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.adapter.StallDealAdapter;
import com.bnlr.lrparking.adapter.StallDealScreenAdapter;
import com.bnlr.lrparking.adapter.StallPriceAdapter;
import com.bnlr.lrparking.adapter.SynthesizeAdapter;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.IntentConstant;
import com.bnlr.lrparking.model.AddStallTypeBean;
import com.bnlr.lrparking.model.CarPlaceTypeMode;
import com.bnlr.lrparking.model.ConditionBean;
import com.bnlr.lrparking.model.SpacePriceFilterM;
import com.bnlr.lrparking.model.StallDealScreenM;
import com.bnlr.lrparking.model.StallListBean;
import com.bnlr.lrparking.appui.publish.PublishActivity;
import com.bnlr.lrparking.appui.carport.filtrate.FiltrateBean;
import com.bnlr.lrparking.appui.carport.filtrate.FiltrateConditionAdapter;
import com.bnlr.lrparking.appui.carport.filtrate.FiltrateConditionBean;
import com.bnlr.lrparking.view.CarCommon;
import com.bnlr.lrparking.view.ConditionPopup;
import com.bnlr.lrparking.widget.popcity.CityBean;
import com.bnlr.lrparking.widget.popcity.PopChoseCityAdapter;
import com.bnlr.lrparking.widget.popcity.PopCityJson;
import com.bnlr.lrparking.widget.popcity.PopStallAdapter;
import com.bnlr.lrparking.widget.popcity.PopupCheckBox;
import com.bnlr.lrparking.widget.refresh.RefreshHolderUtil;
import com.bnlr.lrparking.widget.refresh.XRefreshLayout;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.bnlr.nohttp.CustomJavaHttpListener;
import com.bnlr.nohttp.HttpListener;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 车位交易列表
 *
 * @author hp
 * @date 2017/10/9
 */

public class StallDealActivity extends BaseActivity {

    @BindView(R.id.check_city)
    PopupCheckBox check_city;
    @BindView(R.id.check_type)
    PopupCheckBox check_type;
    @BindView(R.id.check_price)
    PopupCheckBox check_price;
    @BindView(R.id.check_synthesize)
    PopupCheckBox check_synthesize;
    @BindView(R.id.recy_stalldeal)
    RecyclerView recy_stalldeal;
    @BindView(R.id.recy_screen)
    RecyclerView recy_screen;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.image_stall_back)
    ImageView imageStallBack;
    @BindView(R.id.stall_search)
    RelativeLayout rlSearch;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.rely_area)
    RelativeLayout rely_area;
    @BindView(R.id.swipe_deal)
    XRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_publish)
    TextView tvPublish;
    @BindView(R.id.tv_condition)
    TextView tvCondition;
    @BindView(R.id.rl_check_come)
    RelativeLayout rl_check_come;
    @BindView(R.id.check_come)
    PopupCheckBox pcbFrom;
    TextView plot_name;
    private TextView et_title_search;
    private Unbinder unbinder;
    private RecyclerView rvCity;
    private RecyclerView rvArea;
    private PopChoseCityAdapter adapter1;
    private PopChoseCityAdapter adapter2;
    private PopStallAdapter popStallAdapter1;
    private PopStallAdapter popStallAdapter2;
    private StallDealAdapter adapter;
    private StallDealScreenAdapter stallDealScreenAdapter;
    /**
     * 新增
     */
    private List<String> mlist = new ArrayList<>();
    private List<StallDealScreenM> list1 = new ArrayList<>();
    private StallDealScreenM dataBean = new StallDealScreenM();
    private List<StallListBean.DataBean> list = new ArrayList<>();
    private List<CityBean> cityBeen = new ArrayList<>();
    private List<List<CityBean>> areaBeen = new ArrayList<>();
    private List<CityBean> selectOfCity = new ArrayList<>();
    private int currentPage = 1;
    private String dealType = "0";
    private int areaId = 0;
    /**
     * 车位排序
     */
    private int orderByWay = 1;
    /**
     * 车位类型
     */
    private int SpaceType = 0;
    private String keyWords = "";
    private int selectCityId = 370200;
    private boolean isCity = true;
    private ListView rv_stall_chuzu_list;
    private StallPriceAdapter stallpriceAdapter;
    private SynthesizeAdapter synthesizeAdapter;
    private List<SpacePriceFilterM.DataBean> strstallprice = new ArrayList<>();
    private String[] strstallsynthesize;
    private List<ArrayList<CarPlaceTypeMode>> carType2 = new ArrayList<>();
    private List<CarPlaceTypeMode> carType1 = new ArrayList<>();
    private List<CarPlaceTypeMode> selectcarType = new ArrayList<>();
    private List<AddStallTypeBean.DataBeanX> addStallTypeBeen = new ArrayList<>();
    private int pson = 0;
    /**
     * 车位拥有者类型
     * 0是个人 1是开发商
     */
    private String spaceowertype = "";
    private int PriceID = 0;
    /**
     * 开发商ID
     */
    private String developId;
    private String parkingId;
    private String conditionStr = "";
    private boolean isClick = true;
    private List<FiltrateConditionBean> filtrateData = new ArrayList<>();
    private FiltrateConditionAdapter filtrateAdapter;
    private Map paramsMap = new HashMap();
    private Map<String, Integer> tagMap = new HashMap<>();
    private static final int REQUEST_SEARCH_CODE = 1600;
    private int filterType = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_stalldeal);
        unbinder = ButterKnife.bind(this);
        initView();
        initListener();
    }

    private void initListener() {
        tvPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dealType.equals("10")) {
                    startActivity(new Intent(StallDealActivity.this, PublishActivity.class).putExtra("publish_tag", 1));
                } else if (dealType.equals("11")) {
                    startActivity(new Intent(StallDealActivity.this, PublishActivity.class).putExtra("publish_tag", 2));
                }
            }
        });
    }

    private void initView() {

        et_title_search = (TextView) findViewById(R.id.et_title_search);
        plot_name = (TextView) findViewById(R.id.plot_name1);
        Intent intent = getIntent();
        dealType = intent.getStringExtra("dealType");
        parkingId = intent.getStringExtra("parkingId");
        getSpacePriceFilter(dealType);
        spaceowertype = intent.getStringExtra("spaceowertype");
        keyWords = intent.getStringExtra(IntentConstant.INTENT_STALL_DEAL_SEARCH);
        if (TextUtils.isEmpty(keyWords)) {
            keyWords = "";
            if (dealType.equals("10")) {
                et_title_search.setHint("搜索出租车位");
            } else {
                et_title_search.setHint("搜索出售车位");
            }
        } else {
            et_title_search.setText(keyWords);
        }
        boolean isFromSearch = getIntent().getBooleanExtra("from_search", false);
        if (dealType.equals("11") && isFromSearch) {
            //需要添加来源
            rl_check_come.setVisibility(View.VISIBLE);
            initRentOutForm();
        }

        recy_screen.setLayoutManager(new GridLayoutManager(this, 3));
        if (spaceowertype.equals("0")) {
            tvPublish.setVisibility(View.VISIBLE);
            plot_name.setVisibility(View.GONE);
            rlSearch.setVisibility(View.VISIBLE);
            rely_area.setVisibility(View.VISIBLE);

        } else if (spaceowertype.equals("1")) {
            //getConditionList(parkingId);
            plot_name.setVisibility(View.VISIBLE);
            rlSearch.setVisibility(View.GONE);
            rely_area.setVisibility(View.GONE);
            plot_name.setText(intent.getStringExtra("plotName"));
        }
        developId = intent.getStringExtra("developId");
        getCityList();
        //  getCarPlaceType();
        getConditionLimit();
        initSynthesizePop();
        check_city.setText(AppConfig.getInstance().getString("strCity", ""));
        recy_stalldeal.setLayoutManager(new LinearLayoutManager(this));
//        recy_screen.setLayoutManager(new GridLayoutManager(this,3));////
        swipeRefreshLayout.setRefreshViewHolder(RefreshHolderUtil.getRefreshHolder(this));
        swipeRefreshLayout.setDelegate(new XRefreshLayout.BGARefreshLayoutDelegate() {
            @Override
            public void onBGARefreshLayoutBeginRefreshing(XRefreshLayout refreshLayout) {
                currentPage = 1;
                getDealData();
            }

            @Override
            public boolean onBGARefreshLayoutBeginLoadingMore(XRefreshLayout refreshLayout) {
                return false;
            }
        });

        swipeRefreshLayout.beginRefreshing();
        adapter = new StallDealAdapter(list);
        recy_stalldeal.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage++;
                getDealData();
            }
        }, recy_stalldeal);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(StallDealActivity.this, StallDealDetailActivity.class);
                intent.putExtra("stall_id", list.get(position).getId());
                intent.putExtra("isDevelopers", spaceowertype);//开发商车位
                intent.putExtra("dealstallPrice", list.get(position).getPrice());
                startActivity(intent);
            }
        });
        // getDealData();
        rlSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(StallDealActivity.this, SeachActiity.class);
                intent.putExtra("dealType", dealType);
                intent.putExtra(IntentConstant.INTENT_STALL_DEAL_SEARCH, keyWords);
                startActivityForResult(intent, REQUEST_SEARCH_CODE);

            }
        });
        imageStallBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tvCondition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = View.inflate(StallDealActivity.this, R.layout.popup_condition_layout, null);
                RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv_condition);
                recyclerView.setLayoutManager(new GridLayoutManager(StallDealActivity.this, 3));
                final ConditionPopup popup = new ConditionPopup(StallDealActivity.this, view);
                filtrateAdapter = new FiltrateConditionAdapter(filtrateData, tagMap);
                recyclerView.setAdapter(filtrateAdapter);
                TextView sure = (TextView) view.findViewById(R.id.tv_condition_complete);
                TextView clear = (TextView) view.findViewById(R.id.tv_condition_clear);
                popup.show();
                sure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tagMap = filtrateAdapter.getMap();
                        popup.hide();
                        Map map = filtrateAdapter.getParams();
                        if (map.size() > 0) {
                            paramsMap = map;
                        } else {
                            paramsMap.clear();
                        }
                        currentPage = 1;
                        getDealData();
                    }
                });
                clear.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tagMap.clear();
                        for (int i = 0; i < filtrateData.size(); i++) {
                            if (!filtrateData.get(i).isHeader) {
                                filtrateData.get(i).t.setSelected(false);
                            }
                        }
                        filtrateAdapter.notifyDataSetChanged();
                        currentPage = 1;
                        paramsMap.clear();
                        getDealData();
                    }
                });

            }
        });


    }

    private void initRentOutForm() {
        spaceowertype = "2";
        View view = View.inflate(this, R.layout.pop_stall_chuzu_list, null);
        ListView listView = (ListView) view.findViewById(R.id.rv_stall_chuzu_list);
        final String[] fromData = new String[]{"不限", "个人", "开发商"};
        final SynthesizeAdapter fromAdapter = new SynthesizeAdapter(this, fromData);
        listView.setAdapter(fromAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                fromAdapter.setSelectPosition(position);
                pcbFrom.setText(fromData[position]);
                switch (position) {
                    case 0:
                        spaceowertype = "2";
                        break;
                    case 1:
                        spaceowertype = "0";
                        break;
                    case 2:
                        spaceowertype = "1";
                        break;
                }
                pcbFrom.hidePopup();
                swipeRefreshLayout.beginRefreshing();
            }
        });
        pcbFrom.setPopupView(view, 400);
    }

    /**
     * 获取筛选条件
     */
    private void getConditionLimit() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.BASE_JAVA_URL + "system/getLimit", LrConstant.POST);
            mRequest.add("type", filterType);
            // 添加到请求队列+
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomJavaHttpListener<ConditionBean>(this, true, ConditionBean.class) {
                        @Override
                        public void doWork(ConditionBean data, int code) {
                            List<ConditionBean.ModelBean> modelBeans = data.getModel();
                            List<FiltrateConditionBean> list = new ArrayList<>();
                            for (int i = 0; i < modelBeans.size(); i++) {
                                list.add(new FiltrateConditionBean(true, modelBeans.get(i).getDesc()));
                                for (int j = 0; j < modelBeans.get(i).getData().size(); j++) {
                                    ConditionBean.ModelBean.DataBean cmd = modelBeans.get(i).getData().get(j);
                                    list.add(new FiltrateConditionBean(new FiltrateBean
                                            (modelBeans.get(i).getKey(), cmd.getValue(), cmd.getTxt(), false)));
                                }


                            }
                            filtrateData.clear();
                            filtrateData.addAll(list);
                            //filtrateAdapter.notifyDataSetChanged();


                        }

                        @Override
                        public void onFinally(String obj, int code, boolean isSucceed) throws JSONException {
                            super.onFinally(obj, code, isSucceed);

                        }
                    }, false);
        } catch (Exception e) {
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        if (requestCode == REQUEST_SEARCH_CODE && resultCode == IntentConstant.SEARCH_RESULT_CODE) {
            keyWords = data.getStringExtra(IntentConstant.INTENT_STALL_DEAL_SEARCH);
            if (TextUtils.isEmpty(keyWords)) {
                keyWords = "";
                if (dealType.equals("10")) {
                    et_title_search.setHint("搜索出租车位");
                } else {
                    et_title_search.setHint("搜索出售车位");
                }
            }
            et_title_search.setText(keyWords);
            currentPage = 1;
            getDealData();
        }

    }


    /**
     * 获取列表数据
     */
    private void getDealData() {
        /**
         * spaceowertype:0是个人1开发商
         * areaId:地区id
         * type：类型
         * keys：关键字
         * orderBy:排序方式
         * SpaceType：车位类型
         */
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP_SHOW, LrConstant.POST);
            mRequest.add("action", "GetShowInfoTable");
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("pageIndex", currentPage);
            mRequest.add("SpaceType", SpaceType);
            mRequest.add("spaceOwnerType", spaceowertype);//0是个人1是开发商
            mRequest.add("PriceID", PriceID);
            mRequest.add("parkingId", parkingId);
            if (areaId == 0) {
                mRequest.add("cityName", AppConfig.getInstance().getString("strCity", ""));
                Log.e("城市", AppConfig.getInstance().getString("strCity", ""));
            } else {
                if (isCity) {
                    mRequest.add("cityId", areaId);
                } else {
                    mRequest.add("areaId", areaId);
                }
            }
            mRequest.add("type", dealType);
            mRequest.add("keyWord", keyWords);
            mRequest.add("keyWord1", conditionStr);
            mRequest.add("orderBy", orderByWay);
            if (paramsMap != null && paramsMap.size() > 0) {
                for (Object key : paramsMap.keySet()
                        ) {
                    List<String> value = (List<String>) paramsMap.get(key);
                    String values = value.toString();
                    String result = values.replaceAll("\\[", "").replaceAll("]", "")
                            .replaceAll(" ", "");
                    mRequest.add((String) key, result);
                }
            }
            // 添加到请求队列+
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomHttpListener<StallListBean>(this, true, StallListBean.class) {
                        @Override
                        public void doWork(StallListBean data, String code) {
                            if (TextUtils.equals("1", code)) {
                                if (adapter != null) {
                                    if (data != null && data.getData().size() != 0) {
                                        noData.setVisibility(View.GONE);
                                        if (currentPage == 1) {
                                            list = data.getData();
                                            adapter.setNewData(data.getData());
                                        } else {
                                            list.addAll(data.getData());
                                            adapter.notifyDataSetChanged();
                                        }
                                        adapter.loadMoreComplete();
                                    } else {
                                        if (currentPage == 1) {
                                            list.clear();
                                            adapter.notifyDataSetChanged();
                                            noData.setVisibility(View.VISIBLE);
                                        } else {
                                            adapter.loadMoreEnd();
                                        }
                                    }
                                }
                            } else {
                                ToastUtils.showShort(data.getMsg() + "");
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if (swipeRefreshLayout != null) {
                                    swipeRefreshLayout.endRefreshing();
                                }
                                if ("1".equals(code)) {
                                } else {
                                    if (obj.getString("msg").contains("用户已在其他手机上登录")) {
                                        CarCommon carCommon = new CarCommon();
                                        carCommon.setClickSureListener(new CarCommon.ClickSureListener() {
                                            @Override
                                            public void setSure() {
                                                Intent intent = new Intent(StallDealActivity.this, VerificaActivity.class);
                                                startActivity(intent);
                                                finish();
                                            }
                                        });
                                        carCommon.CommonDialog(StallDealActivity.this, "登录信息已过期，是否重新登录？", "下线通知");
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, false);
        } catch (Exception e) {
        }
    }

    /**
     * 获取运营城市和市区列表
     */
    private void getCityList() {
        //
        try {
            mRequest = NoHttp.createStringRequest(Urls.RUNNING_CITY_URL, LrConstant.GET);
            // 添加到请求队列+
            CallServer.getRequestInstance().add(this, mRequest,
                    new HttpListener<String>() {
                        @Override
                        public void onSucceed(int what, Response<String> response) {
                            String json = response.get();
                            ArrayList<PopCityJson> jsonBean = parseData(json);
                            cityBeen = new ArrayList<>();
                            areaBeen = new ArrayList<>();
                            String city = AppConfig.getInstance().getString("strCity", "郑州市");
                            for (int i = 0; i < jsonBean.size(); i++) {
                                if (jsonBean.get(i).getName().equals(city) || jsonBean.get(i).getName().contains(city)) {
                                    cityBeen.add(new CityBean(jsonBean.get(i).getId(), jsonBean.get(i).getId(), jsonBean.get(i).getName()));
                                    if (jsonBean.get(i).getAreas() == null || jsonBean.get(i).getAreas().size() == 0) {
                                        List<CityBean> di = new ArrayList<>();
                                        di.add(new CityBean(jsonBean.get(i).getId(), jsonBean.get(i).getId(), "全地区"));
                                        areaBeen.add(di);
                                    } else {
                                        List<CityBean> cityArr = new ArrayList<>();
                                        cityArr.add(new CityBean(jsonBean.get(i).getId(), jsonBean.get(i).getId(), "全地区"));
                                        int length = jsonBean.get(i).getAreas().size();
                                        for (int c = 0; c < length; c++) {
                                            if (!jsonBean.get(i).getAreas().get(c).getName().equals("市辖区")) {
                                                cityArr.add(new CityBean(jsonBean.get(i).getId(), jsonBean.get(i).getAreas().get(c).getId(), jsonBean.get(i).getAreas().get(c).getName()));
                                            }
                                        }
                                        areaBeen.add(cityArr);
                                        break;
                                    }
                                }
                            }
                            initPop();
                        }

                        @Override
                        public void onFailed(int what, Response<String> response) {

                        }
                    }, false);
        } catch (Exception e) {
        }
    }

    private ArrayList<PopCityJson> parseData(String result) {
        ArrayList<PopCityJson> detail = new ArrayList<>();
        try {
            JSONArray data = new JSONArray(result);
            Gson gson = new Gson();
            for (int i = 0; i < data.length(); i++) {
                PopCityJson entity = gson.fromJson(data.optJSONObject(i).toString(), PopCityJson.class);
                detail.add(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return detail;
    }

    /**
     * 地区初始化
     */
    private void initPop() {

        Log.i("TAG", "this is " + this);
        View view = View.inflate(this, R.layout.pop_window_layout, null);
        Log.i("TAG", "initPopView  start!");
        Log.i("TAG", "view is    ------------>" + view);
        initPopView(view);
        if (check_city != null) {
            check_city.setPopupView(view, 800);
        }
        Log.i("TAG", "setPopupView over !");
    }

    private void initPopView(View mView) {
        Log.i("TAG", "method initPopView   start!");
        rvCity = (RecyclerView) mView.findViewById(R.id.rv_city);
        rvArea = (RecyclerView) mView.findViewById(R.id.rv_area);
        initRecyclerView(rvCity);
        initRecyclerView(rvArea);
        adapter1 = new PopChoseCityAdapter(cityBeen);
        rvCity.setAdapter(adapter1);
        adapter2 = new PopChoseCityAdapter(selectOfCity);
        rvArea.setAdapter(adapter2);
        adapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                adapter1.setPressPostion(position);
                adapter1.notifyDataSetChanged();
                selectOfCity = areaBeen.get(position);
                adapter2.setPressPostion(-1);
                adapter2.setNewData(selectOfCity);
            }
        });

        adapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                adapter2.setPressPostion(position);
                adapter2.notifyDataSetChanged();
                CityBean data = selectOfCity.get(position);
                check_city.setText(data.getName());
                check_city.hidePopup();
                areaId = data.getId();
                selectCityId = data.getCityId();
                currentPage = 1;
                if (selectCityId == areaId) {
                    isCity = true;
                } else {
                    isCity = false;
                }
                getDealData();
            }
        });
        adapter1.setPressPostion(0);
        adapter1.notifyDataSetChanged();
        adapter2.setPressPostion(0);
        if (areaBeen.size() != 0) {
            selectOfCity = areaBeen.get(0);
            adapter2.setNewData(areaBeen.get(0));
            adapter2.notifyDataSetChanged();
        }
    }


    /**
     * 价格初始化
     */
    private void initPricePop() {
        View view = View.inflate(this, R.layout.pop_stall_chuzu_list, null);
        initPricePopView(view);
        if (check_price != null){
            if (dealType.equals("10")) {
                check_price.setPopupView(view, 650);
            } else if (dealType.equals("11")) {
                check_price.setPopupView(view, 800);
            }
        }else {
            Log.i("TAG", "check_price is null!!!!");
        }

    }

    /**
     * 获取筛选价格条件
     *
     * @param type
     */
    private void getSpacePriceFilter(String type) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP_SHOW, LrConstant.POST);
            mRequest.add("action", "GetSpacePriceFilter");
            mRequest.add("type", type);
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<SpacePriceFilterM>(baseContext, true, SpacePriceFilterM.class) {
                        @Override
                        public void doWork(SpacePriceFilterM data, String code) {
                            if (code.equals("1")) {
                                strstallprice = data.getData();
                                initPricePop();
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if (!code.equals("1")) {
                                    ToastUtils.showShort(obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, false);
        } catch (Exception e) {

        }

    }

    private void initPricePopView(View mView) {
        rv_stall_chuzu_list = (ListView) mView.findViewById(R.id.rv_stall_chuzu_list);
        stallpriceAdapter = new StallPriceAdapter(this, strstallprice);
        rv_stall_chuzu_list.setAdapter(stallpriceAdapter);
        rv_stall_chuzu_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                stallpriceAdapter.setSelectPosition(position);
                check_price.setText(strstallprice.get(position).getTitle());
                check_price.hidePopup();
                currentPage = 1;
                PriceID = strstallprice.get(position).getID();
                getDealData();
            }
        });
    }

    /**
     * 综合初始化
     */
    private void initSynthesizePop() {
        View view = View.inflate(this, R.layout.pop_stall_chuzu_list, null);
        initSynthesizeView(view);
        check_synthesize.setPopupView(view, 650);
    }

    private void initSynthesizeView(View mView) {
        rv_stall_chuzu_list = (ListView) mView.findViewById(R.id.rv_stall_chuzu_list);
        strstallsynthesize = new String[]{"综合排序", "最新发布", "价格由低到高", "面积由大到小", "面积由小到大",};
        synthesizeAdapter = new SynthesizeAdapter(this, strstallsynthesize);
        rv_stall_chuzu_list.setAdapter(synthesizeAdapter);
        rv_stall_chuzu_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                synthesizeAdapter.setSelectPosition(position);
                check_synthesize.setText(strstallsynthesize[position]);
                check_synthesize.hidePopup();
                //排序方式
                if (position == 0) {
                    orderByWay = 0;
                } else if (position == 1) {
                    orderByWay = 1;
                } else if (position == 2) {
                    orderByWay = 4;
                } else if (position == 3) {
                    orderByWay = 5;
                } else if (position == 4) {
                    orderByWay = 6;
                }
                currentPage = 1;
                getDealData();
            }
        });
    }

    private void initRecyclerView(RecyclerView recyclerView) {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    protected void onStop() {
        super.onStop();
        // activity 销毁前取消所有请求
        CallServer.getRequestInstance().cancelBySign(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

//        CallServer.getRequestInstance().cancelBySign(this);

        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    //车位类型
    private void getCarPlaceType() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.SPACE_IP, LrConstant.POST);
            mRequest.add("action", "GetSpaceType");
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<AddStallTypeBean>(baseContext, true, AddStallTypeBean.class) {
                        @Override
                        public void doWork(AddStallTypeBean data, String code) {
                            if (code.equals("1")) {
                                addStallTypeBeen.addAll(data.getData());
                                for (int i = 0; i < data.getData().size(); i++) {
                                    carType1.add(new CarPlaceTypeMode(data.getData().get(i).getKey()
                                            , data.getData().get(i).getValue()));
                                    ArrayList<CarPlaceTypeMode> type1 = new ArrayList<>();
                                    for (int j = 0; j < data.getData().get(i).getData().size(); j++) {
                                        type1.add(new CarPlaceTypeMode
                                                (data.getData().get(i).getData().get(j).getKey(), data.getData().get(i).getData().get(j).getValue()));
                                    }
                                    carType2.add(type1);
                                }
                                initstallTypePop();
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if (!code.equals("1")) {
                                    ToastUtils.showShort(obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, false);
        } catch (Exception e) {

        }
    }


    //获取条件列表
    private void getConditionList(String Id) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "GetParkingKeyWord");
            mRequest.add("parkingId", Id);

            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<StallDealScreenM>(baseContext, true, StallDealScreenM.class) {
                        @Override
                        public void doWork(final StallDealScreenM data, String code) {
                            if (code.equals("1")) {
                                List<String> arrayList = new ArrayList<>();
                                arrayList = data.getData();
                                if (arrayList.size() <= 0) {
                                    viewLine.setVisibility(View.GONE);
                                } else {
                                    viewLine.setVisibility(View.VISIBLE);
                                }
                                stallDealScreenAdapter = new StallDealScreenAdapter(arrayList);
                                recy_screen.setAdapter(stallDealScreenAdapter);
                                stallDealScreenAdapter.setListClickListener(new StallDealScreenAdapter.ListClickListener() {
                                    @Override
                                    public void onItemClick(View view) {
                                        conditionStr = stallDealScreenAdapter.getList().toString().replace("[", "").replace("]", "").replace(" ", "");
                                        currentPage = 1;
                                        getDealData();

                                    }
                                });

                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if (!code.equals("1")) {
                                    ToastUtils.showShort(obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, false);
        } catch (Exception e) {

        }
    }


    /**
     * 车位类型初始化
     */
    private void initstallTypePop() {
        View view = View.inflate(this, R.layout.pop_window_layout, null);
        initstallTypePopView(view);
        check_type.setPopupView(view, 800);
    }

    private void initstallTypePopView(View mView) {
        rvCity = (RecyclerView) mView.findViewById(R.id.rv_city);
        rvArea = (RecyclerView) mView.findViewById(R.id.rv_area);
        initRecyclerView(rvCity);
        initRecyclerView(rvArea);
        popStallAdapter1 = new PopStallAdapter(carType1);
        rvCity.setAdapter(popStallAdapter1);
        popStallAdapter2 = new PopStallAdapter(selectcarType);
        rvArea.setAdapter(popStallAdapter2);

        popStallAdapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                popStallAdapter1.setPressPostion(position);
                popStallAdapter1.notifyDataSetChanged();
                selectcarType = carType2.get(position);
                popStallAdapter2.setPressPostion(-1);
                popStallAdapter2.setNewData(selectcarType);
                pson = position;
            }
        });

        popStallAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                popStallAdapter2.setPressPostion(position);
                popStallAdapter2.notifyDataSetChanged();
                String data = selectcarType.get(position).getValue();
                check_type.setText(data);
                check_type.hidePopup();
                SpaceType = addStallTypeBeen.get(pson).getData().get(position).getKey();
                currentPage = 1;
                getDealData();
            }
        });

        popStallAdapter1.setPressPostion(0);
        popStallAdapter1.notifyDataSetChanged();
        popStallAdapter2.setPressPostion(0);
        if (carType2.size() != 0) {
            selectcarType = carType2.get(0);
            popStallAdapter2.setNewData(carType2.get(0));
            popStallAdapter2.notifyDataSetChanged();
        }
    }
}
